<template>
  <el-card class="setting">
    <el-tabs>
      <el-tab-pane>
        <template #label>
          <div class="title">角色管理</div>
        </template>
        <el-button
          type="primary"
          class="add"
          size="mini"
          @click="add"
        >+ 新增角色</el-button>
        <el-table :data="roles" border>
          <el-table-column label="序号" width="150px">
            <template v-slot="{ $index }">
              <div>
                {{ (page.page - 1) * page.pagesize + $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="角色名" prop="name" width="250px" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="300px">
            <template v-slot="{ row }">
              <div class="btn">
                <el-button
                  type="text"
                  @click="assignPerm(row)"
                >分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            :current-page="page.page"
            :page-sizes="[1, 5, 10]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @current-change="pageChange"
            @size-change="sizeChange"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane lazy>
        <template #label>
          <div class="title">公司信息</div>
        </template>
        <!-- 动态组件 -->
        <component :is="'CompanyInfo'" />
      </el-tab-pane>
    </el-tabs>
    <!-- 修改弹框 -->
    <DialogItem
      ref="dialogItem"
      :is-show.sync="isShow"
      :roles="roles"
      @pageChange="pageChange"
      @getRole="getRole"
    />
    <!-- 分配权限弹框 -->
    <DialogPerm ref="dialogPerm" />
  </el-card>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting.js'
import DialogItem from './components/dialog-item.vue'
import DialogPerm from './components/dialog-perm.vue'
export default {
  name: 'Setting',
  components: {
    DialogItem,
    // 异步组件
    CompanyInfo: () => import('./components/company-info.vue'),
    DialogPerm
  },
  data() {
    return {
      roles: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      isShow: false
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    // 获取所有角色数据
    async getRole() {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.roles = res.data.rows
    },
    // 分页导航页码改变
    pageChange(page) {
      this.page.page = page
      this.getRole(this.page)
    },
    // 分页导航页容量改变
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getRole(this.page)
    },
    add() {
      this.isShow = true
      this.$refs.dialogItem.mode = 'add'
    },
    // 删除角色
    del(row) {
      this.$confirm('您确定要删除该角色吗？', '温馨提示').then(async() => {
        await sysRoleDel(row.id)
        this.$message.success('删除角色成功')
        this.pageChange(1)
      })
    },
    // 修改角色
    edit(row) {
      this.isShow = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    // 分配权限点击
    assignPerm(row) {
      this.$refs.dialogPerm.isShow = true
      this.$refs.dialogPerm.getRoleInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  margin: 20px;
  ::v-deep .el-tabs__header {
    .title {
      padding: 0 20px;
      font-size: 18px;
    }
  }
  ::v-deep .el-tabs__content {
    padding: 30px 40px 0;
    .add {
      margin-bottom: 20px;
    }
    thead > tr > th:nth-child(4) {
      text-align: center;
    }
    .btn,
    .pagination {
      text-align: center;
    }
  }
}
</style>
